package com.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class JWTSignalUtils {


//    private final String secretKey="FALUNDAFAHAO" ;
//    private final int expireTime=2 ;

    //生成铜啃
    public static String getToken(String userName,String password) {
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.HOUR,2);
        //JWT之生成，在于头，负载和签名。
        Map<String,Object> maps=new HashMap<>();
        String token=
                JWT.create().withHeader(maps).withClaim("userName",userName)
                        .withClaim("password",password)
                        .withExpiresAt(calendar.getTime())
                        .sign(Algorithm.HMAC256("FALUNDAFAHAO"));
      return token;
    }
    //huoqutoken
    public static String getTokenValue(String token,String key){
        JWTVerifier jwtVerifier= JWT.require(Algorithm.HMAC256("FALUNDAFAHAO")).build();
        DecodedJWT decodedJWT= jwtVerifier.verify(token);
        return decodedJWT.getClaim(key).asString();
    }
}
